﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ConsolidationTool.Common.Entities;
using System.Collections.Generic;
using ConsolidationTool.Common;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ConsolidationTool.WebParts.ControlTemplates;
using System.Linq;

namespace ConsolidationTool.WebParts.Layouts.CReport.Flash
{
    public partial class SCS : FSReportBase
    {
        public override ControlTemplates.CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }

        public override void BeforLoad()
        {
            if (!IsPostBack)
            {
                InitBaseInfo();
            }
        }
 
        void InitBaseInfo()
        {
            if (!Automatic)
            {
                TopSelector.yearTextBox.Text = Common.BusinessService.GetConfiguration(Constants.Flash_Input_Year);
                TopSelector.monthTextBox.Text = Common.BusinessService.GetConfiguration(Constants.Flash_Input_Month);
            }
            Title_HRM1.Text = TopSelector.yearTextBox.Text + ".01";
            Title_HRM2.Text = TopSelector.yearTextBox.Text + ".02";
            Title_HRM3.Text = TopSelector.yearTextBox.Text + ".03";
            Title_HRM4.Text = TopSelector.yearTextBox.Text + ".04";
            Title_HRM5.Text = TopSelector.yearTextBox.Text + ".05";
            Title_HRM6.Text = TopSelector.yearTextBox.Text + ".06";
            Title_HRM7.Text = TopSelector.yearTextBox.Text + ".07";
            Title_HRM8.Text = TopSelector.yearTextBox.Text + ".08";
            Title_HRM9.Text = TopSelector.yearTextBox.Text + ".09";
            Title_HRM10.Text = TopSelector.yearTextBox.Text + ".10";
            Title_HRM11.Text = TopSelector.yearTextBox.Text + ".11";
            Title_HRM12.Text = TopSelector.yearTextBox.Text + ".12";
        }

        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            Title_HRM1.Text = TopSelector.yearTextBox.Text + ".01";
            Title_HRM2.Text = TopSelector.yearTextBox.Text + ".02";
            Title_HRM3.Text = TopSelector.yearTextBox.Text + ".03";
            Title_HRM4.Text = TopSelector.yearTextBox.Text + ".04";
            Title_HRM5.Text = TopSelector.yearTextBox.Text + ".05";
            Title_HRM6.Text = TopSelector.yearTextBox.Text + ".06";
            Title_HRM7.Text = TopSelector.yearTextBox.Text + ".07";
            Title_HRM8.Text = TopSelector.yearTextBox.Text + ".08";
            Title_HRM9.Text = TopSelector.yearTextBox.Text + ".09";
            Title_HRM10.Text = TopSelector.yearTextBox.Text + ".10";
            Title_HRM11.Text = TopSelector.yearTextBox.Text + ".11";
            Title_HRM12.Text = TopSelector.yearTextBox.Text + ".12";

            //邦定current月数据
            int m = Convert.ToInt32(TopSelector.monthTextBox.Text);

            for (int i = 1; i <= m; i++)
            {
                string strMonth = i.ToString();
                BindLastMonth(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text, strMonth);
            }

        }

        protected void ExportReport_Click(object sender, EventArgs e)
        {
            P6PL col01 = null;
            col01 = new P6PL();
            col01.row01 = Label_HRR01_C01.Text;
            col01.row02 = Label_HRR02_C01.Text;
            col01.row03 = Label_HRR03_C01.Text;
            col01.row04 = Label_HRR04_C01.Text;
            col01.row05 = Label_HRR05_C01.Text;
            col01.row06 = Label_HRR06_C01.Text;
            col01.row07 = Label_HRR07_C01.Text;
            col01.row08 = Label_HRR08_C01.Text;
            col01.row09 = Label_HRR09_C01.Text;
            col01.row10 = Label_HRR10_C01.Text;

            P6PL col02 = null;
            col02 = new P6PL();
            col02.row01 = Label_HRR01_C02.Text;
            col02.row02 = Label_HRR02_C02.Text;
            col02.row03 = Label_HRR03_C02.Text;
            col02.row04 = Label_HRR04_C02.Text;
            col02.row05 = Label_HRR05_C02.Text;
            col02.row06 = Label_HRR06_C02.Text;
            col02.row07 = Label_HRR07_C02.Text;
            col02.row08 = Label_HRR08_C02.Text;
            col02.row09 = Label_HRR09_C02.Text;
            col02.row10 = Label_HRR10_C02.Text;

            P6PL col03 = null;
            col03 = new P6PL();
            col03.row01 = Label_HRR01_C03.Text;
            col03.row02 = Label_HRR02_C03.Text;
            col03.row03 = Label_HRR03_C03.Text;
            col03.row04 = Label_HRR04_C03.Text;
            col03.row05 = Label_HRR05_C03.Text;
            col03.row06 = Label_HRR06_C03.Text;
            col03.row07 = Label_HRR07_C03.Text;
            col03.row08 = Label_HRR08_C03.Text;
            col03.row09 = Label_HRR09_C03.Text;
            col03.row10 = Label_HRR10_C03.Text;

            P6PL col04 = null;
            col04 = new P6PL();
            col04.row01 = Label_HRR01_C04.Text;
            col04.row02 = Label_HRR02_C04.Text;
            col04.row03 = Label_HRR03_C04.Text;
            col04.row04 = Label_HRR04_C04.Text;
            col04.row05 = Label_HRR05_C04.Text;
            col04.row06 = Label_HRR06_C04.Text;
            col04.row07 = Label_HRR07_C04.Text;
            col04.row08 = Label_HRR08_C04.Text;
            col04.row09 = Label_HRR09_C04.Text;
            col04.row10 = Label_HRR10_C04.Text;

            P6PL col05 = null;
            col05 = new P6PL();
            col05.row01 = Label_HRR01_C05.Text;
            col05.row02 = Label_HRR02_C05.Text;
            col05.row03 = Label_HRR03_C05.Text;
            col05.row04 = Label_HRR04_C05.Text;
            col05.row05 = Label_HRR05_C05.Text;
            col05.row06 = Label_HRR06_C05.Text;
            col05.row07 = Label_HRR07_C05.Text;
            col05.row08 = Label_HRR08_C05.Text;
            col05.row09 = Label_HRR09_C05.Text;
            col05.row10 = Label_HRR10_C05.Text;

            P6PL col06 = null;
            col06 = new P6PL();
            col06.row01 = Label_HRR01_C06.Text;
            col06.row02 = Label_HRR02_C06.Text;
            col06.row03 = Label_HRR03_C06.Text;
            col06.row04 = Label_HRR04_C06.Text;
            col06.row05 = Label_HRR05_C06.Text;
            col06.row06 = Label_HRR06_C06.Text;
            col06.row07 = Label_HRR07_C06.Text;
            col06.row08 = Label_HRR08_C06.Text;
            col06.row09 = Label_HRR09_C06.Text;
            col06.row10 = Label_HRR10_C06.Text;

            P6PL col07 = null;
            col07 = new P6PL();
            col07.row01 = Label_HRR01_C07.Text;
            col07.row02 = Label_HRR02_C07.Text;
            col07.row03 = Label_HRR03_C07.Text;
            col07.row04 = Label_HRR04_C07.Text;
            col07.row05 = Label_HRR05_C07.Text;
            col07.row06 = Label_HRR06_C07.Text;
            col07.row07 = Label_HRR07_C07.Text;
            col07.row08 = Label_HRR08_C07.Text;
            col07.row09 = Label_HRR09_C07.Text;
            col07.row10 = Label_HRR10_C07.Text;

            P6PL col08 = null;
            col08 = new P6PL();
            col08.row01 = Label_HRR01_C08.Text;
            col08.row02 = Label_HRR02_C08.Text;
            col08.row03 = Label_HRR03_C08.Text;
            col08.row04 = Label_HRR04_C08.Text;
            col08.row05 = Label_HRR05_C08.Text;
            col08.row06 = Label_HRR06_C08.Text;
            col08.row07 = Label_HRR07_C08.Text;
            col08.row08 = Label_HRR08_C08.Text;
            col08.row09 = Label_HRR09_C08.Text;
            col08.row10 = Label_HRR10_C08.Text;

            P6PL col09 = null;
            col09 = new P6PL();
            col09.row01 = Label_HRR01_C09.Text;
            col09.row02 = Label_HRR02_C09.Text;
            col09.row03 = Label_HRR03_C09.Text;
            col09.row04 = Label_HRR04_C09.Text;
            col09.row05 = Label_HRR05_C09.Text;
            col09.row06 = Label_HRR06_C09.Text;
            col09.row07 = Label_HRR07_C09.Text;
            col09.row08 = Label_HRR08_C09.Text;
            col09.row09 = Label_HRR09_C09.Text;
            col09.row10 = Label_HRR10_C09.Text;

            P6PL col10 = null;
            col10 = new P6PL();
            col10.row01 = Label_HRR01_C10.Text;
            col10.row02 = Label_HRR02_C10.Text;
            col10.row03 = Label_HRR03_C10.Text;
            col10.row04 = Label_HRR04_C10.Text;
            col10.row05 = Label_HRR05_C10.Text;
            col10.row06 = Label_HRR06_C10.Text;
            col10.row07 = Label_HRR07_C10.Text;
            col10.row08 = Label_HRR08_C10.Text;
            col10.row09 = Label_HRR09_C10.Text;
            col10.row10 = Label_HRR10_C10.Text;

            P6PL col11 = null;
            col11 = new P6PL();
            col11.row01 = Label_HRR01_C11.Text;
            col11.row02 = Label_HRR02_C11.Text;
            col11.row03 = Label_HRR03_C11.Text;
            col11.row04 = Label_HRR04_C11.Text;
            col11.row05 = Label_HRR05_C11.Text;
            col11.row06 = Label_HRR06_C11.Text;
            col11.row07 = Label_HRR07_C11.Text;
            col11.row08 = Label_HRR08_C11.Text;
            col11.row09 = Label_HRR09_C11.Text;
            col11.row10 = Label_HRR10_C11.Text;

            P6PL col12 = null;
            col12 = new P6PL();
            col12.row01 = Label_HRR01_C12.Text;
            col12.row02 = Label_HRR02_C12.Text;
            col12.row03 = Label_HRR03_C12.Text;
            col12.row04 = Label_HRR04_C12.Text;
            col12.row05 = Label_HRR05_C12.Text;
            col12.row06 = Label_HRR06_C12.Text;
            col12.row07 = Label_HRR07_C12.Text;
            col12.row08 = Label_HRR08_C12.Text;
            col12.row09 = Label_HRR09_C12.Text;
            col12.row10 = Label_HRR10_C12.Text;

            P6PL colHead = null;
            colHead = new P6PL();
            colHead.row01 = TopSelector.drpRegion.Text;
            colHead.row02 = TopSelector.drpSubRegion.Text;
            colHead.row03 = TopSelector.lbCompanyFullName.Text;
            colHead.row04 = TopSelector.lbCoCd.Text;
            colHead.row05 = TopSelector.lbCompanyFullName.Text;
            colHead.row06 = TopSelector.yearTextBox.Text;
            colHead.row07 = TopSelector.monthTextBox.Text;
            colHead.row08 = TopSelector.lbCompanyCurrency.Text;
            colHead.row09 = TopSelector.lbCompanyTonnage.Text;
            colHead.row11 = Title_HRM1.Text;
            colHead.row12 = Title_HRM2.Text;
            colHead.row13 = Title_HRM3.Text;
            colHead.row14 = Title_HRM4.Text;
            colHead.row15 = Title_HRM5.Text;
            colHead.row16 = Title_HRM6.Text;
            colHead.row17 = Title_HRM7.Text;
            colHead.row18 = Title_HRM8.Text;
            colHead.row19 = Title_HRM9.Text;
            colHead.row20 = Title_HRM10.Text;
            colHead.row21 = Title_HRM11.Text;
            colHead.row22 = Title_HRM12.Text;

            var rowData = new Dictionary<string, List<string>>();// upgrade to export report by mike 0802 imp
            for (int i = 1; i <=10; i++)
            {
                string r = fixNum(i);
                var values = new List<string>();
                for (int a = 1; a <= 12; a++)
                {
                    string b = fixNum(a);
                    string value = this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR" + r + "_C" + b).Text;
                    values.Add(value);
                }
                rowData.Add(r, values);
            }

            string fileName = TopSelector.yearTextBox.Text + TopSelector.monthTextBox.Text + Constants._ReportTemplate_FlashSCS + TopSelector.lbCoCd.Text + ".xlsx";
            BusinessService.CreateSCSExcel(rowData,col01, col02, col03, col04, col05, col06, col07, col08, col09, col10, col11, col12, fileName, colHead);
    
            string reportUrl = SPContext.Current.Site.Url + "/" + Constants._EXCEL_Library + "/" + fileName;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", string.Format("<script>window.open('" + reportUrl + "');</script>"));

        }

        void BindLastMonth(string coCd, string year, string month)
        {
            string y = year;
            string m = (Convert.ToInt32(month)).ToString();
            SPListItem it = Common.BusinessService.GetItem(coCd, y, m, "Sales&Commission&Service");

            if (it != null)
            { 
                //for (int i = 1; i <= 12; i++)
                //{
                    string r = fixNum(month);

                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR01_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01030100");
                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR02_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01030101");
                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR03_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01030102");
                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR04_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01030103");
                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR05_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01040100");
                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR06_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01050100");
                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR07_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01050101");
                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR08_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01050102");
                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR09_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01050103");
                    this.Page.Controls.AllOfType<Label>().FirstOrDefault(p => p.ID == "Label_HRR10_C" + r).Text = Common.Utilities.GetItemValue(it, "SV01060100");
                //}

            }
        }

        private string fixNum(string month)
        {
            return fixNum(int.Parse(month));
        }

        string fixNum(int num) {
            if (num >= 10)
            {
                return num.ToString();
            }
            else
            {
                return "0" + num.ToString();
            }
        }


    }
}
